import cookie from './../tools/cookie.js'
import axios from 'axios'
import url from './config.js'
import md5 from './../tools/md5.js'
import timeout from './timeout.js'

const errMsg='请求不到'

export default {
	login(params){
		return new Promise((resolve,reject)=>{
			axios.post(`${url}/login`,params)
			.then((result)=>{
				var res=result.data;
				if (res.status==1) {
					resolve(res)
					// cookie.setCookie("hasLogin", md5(res.hasLogin), 0.25);

					let wls = window.localStorage
					wls.setItem('studentNumber', params.studentNumber)
				}else{
					cookie.setCookie("hasLogin", md5(0), 0.5);
					reject("密码错误")
				}
			})
			.catch((err) => {
				console.log(err)
				reject(errMsg)
			})
			timeout(reject)
		})	
	},
	out(){
		return new Promise((resolve,reject)=>{
			axios.post(`${url}/login/out`)
			.then((result)=>{
				var res=result.data;
				if (res.status==1) {
					resolve("注销成功！")
				}
			})
			.catch((err) => {
				console.log(err)
				reject(errMsg)
			})
			timeout(reject)
		})
	}
}